package com.zy.imagineCommunity.controller;

import com.alibaba.fastjson.JSONObject;
import com.zy.imagineCommunity.entity.*;
import com.zy.imagineCommunity.service.FriendsService;
import com.zy.imagineCommunity.service.UserSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class FriendsController {

    @Autowired
    FriendsService friendsService;

    @Autowired
    UserSerivce userSerivce;

    @GetMapping("/friends")
    private String getFriends() {
        return "/friends";
    }

    @ResponseBody
    @GetMapping("/addFriend")
    public JSONObject addFriends(String userName, int parentId) {
        JSONObject jsonObject = new JSONObject();

        User user = userSerivce.queryUserByUsername(userName);

        if (user == null) {
            jsonObject.put("msg", "用户不存在!");
            return jsonObject;
        }

        if (user.getId() == parentId) {
            jsonObject.put("msg", "不能添加自己为好友");
            return jsonObject;
        }

        Friends friends = friendsService.queryFriendsByUserIdAndParentId(user.getId(), parentId);

        if (friends == null) {
            //发送好友请求
            friendsService.addFriends(new Friends(null, user.getId(), parentId, 0, 0));
            jsonObject.put("msg", "请求成功!");
        }

        if (friends != null) {
            jsonObject.put("msg", "用户已添加!");
            return jsonObject;
        }

        return jsonObject;
    }

    /**
     * 好友列表
     *
     * @param parentId
     * @param pageNo
     * @return
     */
    @ResponseBody
    @GetMapping("/friendsList")
    public JSONObject friendsList(int parentId, int pageNo) {
        System.out.println("父亲id:" + parentId);
        int applyNum = friendsService.queryApplyCountTotalByUserIdAndStatus(parentId, 0);
        //查询这一页的信息
        Page<FriendsText> page = friendsService.queryFriendsByParentIdAndStatusAndPageNoAndPageSize(parentId, pageNo, 10);

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("page", page);

        jsonObject.put("applyNum", applyNum);

        return jsonObject;
    }

    @GetMapping("/apply")
    private String getApply() {
        return "/applyList";
    }

    /**
     * 申请列表
     *
     * @param userId
     * @param pageNo
     * @return
     */
    @ResponseBody
    @GetMapping("/applyList")
    public JSONObject applyList(int userId, int pageNo) {

        //查询这一页的信息
        Page<FriendsText> page = friendsService.queryApplyByUserIdAndStatusAndPageNoAndPageSize(userId, pageNo, 10);

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("page", page);

        return jsonObject;
    }

    @GetMapping("/agreeApply/{id}")
    public String agreeApply(@PathVariable("id") int id) {

        Friends friends = friendsService.queryFriendsById(id);

        friendsService.updateFriendsById(new Friends(friends.getId(), friends.getUserId(), friends.getParentId(), 1, 0));

        //同意申请后要加入自己的列表
        friendsService.addFriends(new Friends(null, friends.getParentId(), friends.getUserId(), 1, 0));

        return "forward:/apply";
    }

    @GetMapping("/refuseApply/{id}")
    public String refuseApply(@PathVariable("id") int id) {

        friendsService.deleteFriendsOrApplyById(id);
        //forward:跳转方法
        return "forward:/apply";
    }

    @ResponseBody
    @PostMapping("/deleteFriend")
    public void deleteFriend(int userId, int parentId) {

        friendsService.deleteFriend(userId, parentId);

        friendsService.deleteFriend(parentId, userId);

    }

    /**
     * 搜索好友
     *
     * @param parentId
     * @param pageNo
     * @param searchValue
     * @return
     */
    @ResponseBody
    @GetMapping("/searchFriends")
    public JSONObject searchFriends(int parentId, int pageNo, String searchValue) {

        //查询这一页的信息
        Page<FriendsText> page = friendsService.searchValueFriendsByParentIdAndStatusAndPageNoAndPageSizeAndSearchValue(parentId, pageNo, 10, searchValue);
        System.out.println(page);
        JSONObject jsonObject = new JSONObject();

        jsonObject.put("page", page);

        return jsonObject;
    }

}
